A return stack buffer (RSB), which is also known as a return address stack (RAS), is used to predict return addresses from functions. The address following a function call is pushed to the RSB and then subsequently popped when a return is found. However, the RSB can become corrupted where speculation is used because a number of pushes and pops to/from the stack can be performed speculatively. In a processor architecture with prediction and fetch decoupled from execution, a prediction can be made every cycle and so the RSB can become very corrupted such that a single mis-speculation can lead to multiple mis-speculations (as a result of the corrupted RSB).
A solution to the corruption of the RSB as a result of speculatively executed instructions is to store a pointer to the top of the RSB with every prediction that is made. If the prediction turns out to be wrong, then the stored pointer is restored. This preserves alignment of the RSB but information can be lost due to speculative pops from the RSB. To address this, the idea may be extended to store the top of the RSB along with the pointer such that on a mis-speculation at least one speculative pop can be restored using the stored data.
The embodiments described below are not limited to implementations which solve any or all of the disadvantages of known return stack buffers and methods of operating a processor comprising a return stack buffer.